<template>
  <div :id="id" :class="className" :style="{ height: height, width: width }" />
</template>
<script>
import * as echarts from "echarts";
import resize from "../static/resize";
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: "chartline",
    },
    id: {
      type: String,
      default: "chartline",
    },
    width: {
      type: String,
      default: "30%",
    },
    height: {
      type: String,
      default: "100%",
    },
  },
  data() {
    return {
      chart: null,
    };
  },
  mounted() {
    this.initChart();
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  methods: {
    initChart() {
      this.chart = echarts.init(document.getElementById(this.id));
      this.chart.setOption({
        backgroundColor: "#2f4989",
        title: {
          text: "空气质量指数（AQI）",
          top: "top",
          left: "center",
          textStyle: {
            color: "#fff",
            fontSize: "12",
          },
        },
        tooltip: {
          trigger: "axis",
          formatter: "{a} <br/>{b}: {c} ",
        },
        xAxis: {
          data: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12],
          // name: "Mon",
          type: "category",
          boundaryGap: false,
          axisTick: {
            show: false,
          },
        },
        yAxis: {
          type: "value",
          name: "（值）",
          splitLine: {
            //分割线
            show: false, //控制分割线是否显示
          },
          axisLine: {
            //y轴显示
            show: true,
          },
        },
        grid: {
          //设置图表显示在区域的哪个位置，控制图表的位置，可以是具体数值或者百分比
          left: "5%",
          bottom: "5%",
          containLabel: true, //containLabel(grid 区域是否包含坐标轴的刻度标签,默认不包含)为true的情况下,无法使图表紧贴着画布显示,但可以防止标签标签长度动态变化时溢出容器或者覆盖其他组件,将containLabel设置为false即可解决
        },
        series: [
          {
            name: "AQI",
            type: "line",
            // stack: "总量",
            smooth: true, //设置折线为圆滑曲线,false则有转折点
            symbol: "none", //默认显示折点，为none时不显示
            data: [
              120, 132, 101, 134, 90, 230, 210, 120, 132, 101, 134, 90, 230,
              210,
            ],
            areaStyle: {
              color: "rgba(46,82,212,0.2)", //折线区域的背景颜色
            },
            itemStyle: {
              normal: {
                color: "#fff",
                fontSize: 14,
                lineStyle: {
                  width: 2, //折线宽度
                  //折线颜色渐变
                  color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [
                    {
                      offset: 0,
                      color: "rgba(45,105,249,1)",
                    },
                    {
                      offset: 1,
                      color: "rgba(89,205,253,1)",
                    },
                  ]),
                },
              },
            },
          },
        ],
      });
    },
  },
};
</script>
<style scoped>
.chartline{
  padding: 0 5px;
}
</style>